/**
 * 
 */
package xx.services.teach;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Context;

import xx.services.Passport;
import xx.services.util.Constants;
import xx.teach.entity.Meno;

/**
 * @author Administrator 2014-3-19
 */
@Path(Constants.RESTFUL_ROOT_PATH + "/teach/memo")
public class MemoRest extends Passport
{

    /**
     * @param request
     */
    public MemoRest(@Context
    HttpServletRequest request)
    {
        super(request);
    }

    /**
     * 修改memo内容
     * 
     * @param memoId
     * @param content
     * @return
     */
    @PUT
    @Path("/{memo_id}")
    public String updateMemo(@PathParam("memo_id")
    long memoId, @FormParam("content")
    String content)
    {
        try
        {
            Meno memo = new Meno(qr, owner);
            memo.setMenoId(memoId).setContent(content).update(Meno.CONTENT);
            return ok("更新成功");
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

    /**
     * 删除指定memoId的通告，多个memoId之间用逗号“，”或者下划线“_”间隔
     * 
     * @param memoIdArr
     * @return
     */
    @DELETE
    @Path("/{memo_id}")
    public String deleteMemo(@PathParam("memo_id")
    String memoIdArr)
    {
        try
        {
            String[] ids = memoIdArr.split("_|,");
            int count = 0;
            for (String memoId : ids)
            {
                Meno memo = new Meno(qr, owner);
                memo.setMenoId(Long.valueOf(memoId)).delete();
                count++;
            }

            return ok(count);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }
}
